package com.example.sample.activity;

import java.util.ArrayList;
import java.util.List;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.control.MyImageView;

import jp.co.cyberagent.android.gpuimage.GPUImage;
import jp.co.cyberagent.android.gpuimage.GPUImageBrightnessFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageColorInvertFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageGrayscaleFilter;
import jp.co.cyberagent.android.gpuimage.GPUImage.ResponseListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import net.liteheaven.xblib.common.java.ReflectUtil;

public class FilterTransfromActivity extends BaseActivity {
	
	private Spinner spinnerFilter;
	private ImageView ivSrc;
	private ImageView ivDst;
	private MyImageView testIv;
	private Bitmap mSrcBitmap;
	private Handler mHandler = new Handler();
	
	private String [] FILTER_LIST = {
			"a",
			"b",
			"c",
	};	
	
	private class ComboboxAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return FILTER_LIST.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tv = null;
			if (convertView == null){
				tv = new TextView(FilterTransfromActivity.this);
				tv.setTextSize(25);
			}
			else{
				tv = (TextView)convertView;
			}
			String content = FILTER_LIST[position];
			tv.setText(content);
			tv.setTag(position);
			return tv;
		}
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter_transform);
		spinnerFilter = (Spinner)findViewById(R.id.spinnerFilter);
		ivSrc = (ImageView)findViewById(R.id.ivSrc);
		ivDst = (ImageView)findViewById(R.id.ivDst);
		
		testIv = (MyImageView)findViewById(R.id.testIv);
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				testIv.setImageResource(R.drawable.ic_folder);
			}
		}, 1000);
		
		Log.i("cc", "onCreate1");
		BitmapDrawable d = (BitmapDrawable)getResources().getDrawable(R.drawable.model);
		mSrcBitmap = d.getBitmap();
		Log.i("cc", "onCreate2");
		ivSrc.setImageDrawable(d);
		Log.i("cc", "onCreate3");
		
		ComboboxAdapter adapter = new ComboboxAdapter();
		spinnerFilter.setAdapter(adapter);
		spinnerFilter.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				transform(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});		
	}

	private void transform(int position){

		Class<?> clzList[] = { GPUImageGrayscaleFilter.class,
				GPUImageColorInvertFilter.class,
				GPUImageBrightnessFilter.class, };
		Class<?> clz = clzList[position];
		GPUImageFilter filter = (GPUImageFilter) ReflectUtil.invokeConstructor(
				clz, null, null);
		final List<GPUImageFilter> filterList = new ArrayList<GPUImageFilter>();
		filterList.add(filter);
		
		
		new AsyncTask<Void, Void, Void>(){
			
			private Bitmap bm;

			@Override
			protected Void doInBackground(Void... params) {
				ResponseListener<Bitmap> listener = new ResponseListener<Bitmap>() {

					@Override
					public void response(Bitmap item) {
						bm = item;
					}
				};
				GPUImage.getBitmapForMultipleFilters(mSrcBitmap, filterList, listener);
				return null;
			}
			
			protected void onPostExecute(Void result) {
				ivDst.setImageBitmap(bm);
			};
			
		}.execute();
	}
}
